package com.javalong.affairs.module.renwujinzhang.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.listener.MultipartRequest;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.multirequest.MultipartRequestParams;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.ImageEditView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectFileView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class UpdateTaskActivity extends RecycleListActivity {

    private Task task;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, UpdateTaskActivity.class);
        context.startActivity(intent);
    }

    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, UpdateTaskActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }

    @Override
    public void afterInit() {
        getSupportActionBar().setTitle("修改任务");
        task = (Task) getIntent().getSerializableExtra(BundleKey.TASK);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TIME:
                return new ViewHolder(new SelectTimeView(this));
            case MULTI_USER:
                return new ViewHolder(new SelectUserView(this));
            case EDIT:
                return new ViewHolder(new ImageEditView(this));
            case BUTTON:
                return new ViewHolder(mInflater.inflate(R.layout.item_button, parent, false));
            case FILE:
                return new ViewHolder(new SelectFileView(this));
        }
        return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        switch (position) {
            case 0: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setText(task.getTitle());
            }
            break;
            case 1: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setText(task.getContent());
            }
            break;
            case 2: {
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeBegin()));
            }
            break;
            case 3: {
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeEnd()));
            }
            break;
            case 4: {
                ((SelectUserView) holder.itemView).setHintText(task.getReceiveName());
            }
            break;
            case 5: {
                ((SelectUserView) holder.itemView).setHintText(task.getSuperviseId());
            }
            break;
            case 6: {
                ((SelectUserView) holder.itemView).setHintText(task.getAuditId());
            }
            break;
            case 7: {
                ((SelectFileView) holder.itemView).setHintText("选择文件");
            }
            break;
            case 8: {
                Button button = holder.getView(R.id.bt_item);
                button.setText("更新任务");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sendTask();
                    }
                });
            }
            break;
        }
    }

    private void sendTask() {
        Button bt_send = (Button) getRecyclerView().getChildAt(8);
        bt_send.setEnabled(false);
        bt_send.setText("正在发布中...");
        User user = (User) AllData.GetInstance().get(
                R.string.data_user_current_user);
        String url = UrlUtils.getUrl("Task", "update_main");

        File currFile = (File) getValue(7);
        String title = getValue(0).toString();
        String content = getValue(1).toString();
        long startSeconds = (long) getValue(2);
        if (startSeconds == 0) {
            startSeconds = task.getTimeBegin();
        }
        long endSeconds = (long) getValue(3);
        if (endSeconds == 0) {
            endSeconds = task.getTimeEnd();
        }
        String mainUser = getValue(4).toString();
        if (StringUtils.isEmpty(mainUser)) {
            mainUser = task.getReceiveId() + "";
        }
        String superviseUser = getValue(5).toString();
        if (StringUtils.isEmpty(superviseUser)) {
            superviseUser = task.getSuperviseId() + "";
        }
        String auditUser = getValue(6).toString();
        if (StringUtils.isEmpty(auditUser)) {
            auditUser = task.getAuditId() + "";
        }
        if (currFile != null) {
            MultipartRequestParams param = new MultipartRequestParams();
            param.put(RequestKey.ID, task.getId() + "");
            param.put(RequestKey.SENDATTACHMENT, currFile);
            param.put(RequestKey.CONTENT, content);
            param.put(RequestKey.TITLE, title);

            param.put(RequestKey.AUDITIDS, auditUser);
            param.put(RequestKey.RECEIVEID, mainUser);
            param.put(RequestKey.SUPERVISES, superviseUser);

            param.put(RequestKey.TIMEBEGIN, startSeconds + "");
            param.put(RequestKey.TIMEEND, endSeconds + "");
            HttpUtils.addReuqest(new MultipartRequest(url, param,
                    new BaseResponseListener(new TypeToken<User>() {
                    }.getType()) {
                        @Override
                        public void onSuccess(Object obj) {
                            super.onSuccess(obj);
                            finish();
                        }
                    }));
        } else {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put(RequestKey.ID, task.getId() + "");
            param.put(RequestKey.CONTENT, content);
            param.put(RequestKey.TITLE, title);

            param.put(RequestKey.AUDITIDS, auditUser);
            param.put(RequestKey.RECEIVEID, mainUser);
            param.put(RequestKey.SUPERVISES, superviseUser);

            param.put(RequestKey.TIMEBEGIN, startSeconds + "");
            param.put(RequestKey.TIMEEND, endSeconds + "");
            HttpUtils.post(url, param, new BaseResponseListener(
                    new TypeToken<User>() {
                    }.getType()) {
                @Override
                public void onSuccess(Object obj) {
                    super.onSuccess(obj);
                    finish();
                }
            });
        }
    }

    // 判断是否已选
    private boolean checkSelected() {
        String title = getValue(0).toString();
        String content = getValue(1).toString();
        long startSeconds = (long) getValue(2);
        long endSeconds = (long) getValue(3);
        String mainUser = getValue(4).toString();
        String superviseUser = getValue(5).toString();
        String auditUser = getValue(6).toString();
        if (StringUtils.isEmpty(title)) {
            ToastUtils.showMessage("请输入标题");
            return false;
        }
        if (StringUtils.isEmpty(content)) {
            ToastUtils.showMessage("请输入内容");
            return false;
        }
        if (StringUtils.isEmpty(auditUser)) {
            ToastUtils.showMessage("请选择审核人");
            return false;
        }
        if (StringUtils.isEmpty(superviseUser)) {
            ToastUtils.showMessage("请选择监督人");
            return false;
        }
        if (StringUtils.isEmpty(superviseUser)) {
            ToastUtils.showMessage("请选择监督人");
            return false;
        }
        return true;
    }


    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return EDIT;
            case 1:
                return EDIT;
            case 2:
                return TIME;
            case 3:
                return TIME;
            case 4:
                return MULTI_USER;
            case 5:
                return MULTI_USER;
            case 6:
                return MULTI_USER;
            case 7:
                return FILE;
            case 8:
                return BUTTON;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return 9;
    }
}
